{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import gym\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib.animation as animation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "env_name = 'Breakout-v0'\n",
    "env = gym.make(env_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "frames = [] # array to store state space at each step\n",
    "\n",
    "env.reset()\n",
    "done = False\n",
    "for _ in range(300): \n",
    "    #print(done)\n",
    "    frames.append(env.render(mode='rgb_array'))\n",
    "    obs,reward,done, _ = env.step(env.action_space.sample())\n",
    "    if done:\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAM4AAAD8CAYAAAA/rZtiAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvnQurowAAA+hJREFUeJzt3TFOFGEYgOEZQ208gZWFRyAegFB4GTiBJ9hjGA9gQSgsDYcxFsZYUDC2QgIL767MzPI87fDv/s27H/OHWcZpmgbgaV7NvQFYI+FAIBwIhAOBcCAQDgTCgUA4EAgHgqO5NzAMwzCOoz9fYHGmaRrvu2biQCAcCIQDgXAgWMThwBJtNpsnrzk/P9/pNe6u39dr7GoJe7jr7p6e4z3/ZeJAYOI80v+YBnNMtX147k/3JTJxIDBxeLJtU+4lTCQTBwITh622TZA57rPmZuJAYOI80j4+VZfyGmt4z6UzcSAQDgTjEr7J0/M4LJHncWDPFnE44OaTtTFxIBAOBMKBQDgQCAcC4UAgHAiEA4FwIBAOBMKBQDgQCAcC4UCwiMcKtnkJ39PF89vlcRYTBwLhQCAcCIQDgXAgEA4EwoFAOBAIBwLhQCAcCIQDgXAgEA4EwoFAOBAIBwLhQCAcCIQDgXAgEA4EwoFAOBAIB4JVfJPn1enp3FvgAH3fYa2JA4FwIBAOBMKBQDgQrOJU7ebdr7m3ALeYOBAIBwLhQCAcCIQDgXAgWMVx9M/Xf+beAtxi4kAgHAiEA4FwIBAOBOs4VXt/PfcWOEQ/+lITBwLhQCAcCIQDgXAgWMWp2uebt3NvgQN0ssNaEwcC4UAgHAiEA4FwIFjFqdr1l09zb4FDdNL/0YeJA4FwIBAOBMKBQDgQCAeCVRxHf7s4nnsLHKCPJ5u81sSBQDgQCAcC4UAgHAiEA4FwIBAOBMKBQDgQCAcC4UAgHAiEA4FwIBAOBMKBQDgQCAcC4UAgHAiEA4FwIBAOBMKBQDgQCAcC4UAgHAiEA4FwIBAOBMKBQDgQCAcC4UAgHAiEA4FwIBAOBMKBQDgQCAcC4UAgHAiEA4FwIBAOBMKBQDgQCAcC4UAgHAiEA4FwIBAOBMKBQDgQCAcC4UAgHAiEA4FwIBAOBMKBQDgQCAcC4UAgHAiEA4FwIBAOBMKBQDgQCAcC4UAgHAiEA4FwIBAOBMKBQDgQCAcC4UAgHAiEA4FwIBAOBMKBQDgQCAcC4UAgHAiEA4FwIBAOBMKBQDgQHM29gWEYhq9vfs+9Be5xdXq60/rji4s97WT/PlxePvwDZ2f3XjJxIBAOBMKBYBH3OCzXku9R5mTiQGDi8GJtm6bTA9fGaXro8vMYx3H+TcAd0zSN913zqxoEwoFAOBAIBwLhQCAcCIQDgXAgEA4EwoFAOBAIBwLhQCAcCBbxWAGsjYkDgXAgEA4EwoFAOBAIBwLhQCAcCIQDgXAgEA4EwoFAOBAIBwLhQCAcCIQDgXAgEA4EwoFAOBAIBwLhQPAX+CVhcF98yTEAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "patch = plt.imshow(frames[0])\n",
    "plt.axis('off')\n",
    "def animate(i):\n",
    "    patch.set_data(frames[i])\n",
    "anim = animation.FuncAnimation(plt.gcf(), animate, \\\n",
    "        frames=len(frames), interval=10)\n",
    "anim.save('random_agent.gif', writer='imagemagick')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import gym\n",
    "env = gym.make(\"Breakout-v0\")\n",
    "env = gym.wrappers.Monitor(env, 'recording', force=True)\n",
    "observation = env.reset()\n",
    "for _ in range(1000):\n",
    "    #env.render()\n",
    "    action = env.action_space.sample() # your agent here (this takes random actions)\n",
    "    observation, reward, done, info = env.step(action)\n",
    "\n",
    "    if done:\n",
    "        observation = env.reset()\n",
    "env.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:tf2p0]",
   "language": "python",
   "name": "conda-env-tf2p0-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
